﻿
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Models.Common;
using Models.JsonResultModel;
using ServiceWeb.DBGateway;
using Models.Constant;
using NuGet.Configuration;
using ServiceWeb.WebAdmin;
using UtilsBase;
using UtilsORM;
using UtilsORM.MySqlData;
using GatewayServer.Common;

namespace GatewayServer.Controllers
{
    public class ProductModelController : BaseMagerController
    {
        EFYihongIotContext ef = new EFYihongIotContext();
        // GET: Mager_Department
        public IActionResult list(IFormCollection FC, HttpReqModel httpRequest, int PageIndex = 1, int PageSize = 15, string type = "1")
        {
            List<SugarParameter> sugarParameters = new List<SugarParameter>();
            int totalRecord = 0;
            string sqlwhere = " and pm_c_guid='" + LoginUserInfo.userCompanyGuid + "' ";
            if (!string.IsNullOrEmpty(httpRequest.name))
            {
                sqlwhere = sqlwhere + " and s_name like CONCAT('%',@name,'%') ";
                sugarParameters.Add(new SugarParameter("name", httpRequest.name));

            }
            if (!string.IsNullOrEmpty(FC["hid_pageIndex"]))
            {
                PageIndex = int.Parse(FC["hid_pageIndex"].ToString());

            }
            if (!string.IsNullOrEmpty(httpRequest.status))
            {
                //设置状态
                sqlwhere = sqlwhere + " and pm_status=@status";
                sugarParameters.Add(new SugarParameter("status", httpRequest.status));
            }


            string sql = "select  * from iot_product_model where 1= 1  " + sqlwhere;
            DataTable dt = MySqlYihongIot.getPageDataTable(PageIndex, PageSize, sql, sugarParameters, "pm_id desc", ref totalRecord);


            ViewData["DataList"] = dt;

            ViewBag.PageSize = PageSize;
            ViewBag.RecordCount = totalRecord;//总条数
            ViewBag.PageCount = totalRecord % PageSize == 0 ? totalRecord / PageSize : totalRecord / PageSize + 1;//分的页数
            ViewBag.PageIndex = PageIndex;//当前页码
            return View(httpRequest);
        }
        public IActionResult edit(HttpReqModel httpReqModel, iot_product reqProduct)
        {
            ViewBag.p_guid = reqProduct.p_guid;
            if (httpReqModel.order == "upd")
            {

                iot_product iot_Product = ef.iot_product.Where(c => c.p_guid == reqProduct.p_guid).FirstOrDefault();
                ViewBag.p_name = iot_Product.p_name;
                ViewBag.p_code = iot_Product.p_code;

                iot_product_model model = ef.iot_product_model.Where(c => c.pm_guid == httpReqModel.guid).FirstOrDefault();
                ViewBag.pm_name = model.pm_name;
                ViewBag.pm_guid = httpReqModel.guid.ToString();
            }
            else
            {
                iot_product iot_Product = ef.iot_product.Where(c => c.p_guid == reqProduct.p_guid).FirstOrDefault();
                ViewBag.p_code = iot_Product.p_code;
                ViewBag.p_name = iot_Product.p_name;
                ViewBag.pm_name = iot_Product.p_name;
                ViewBag.pm_guid = Guid.NewGuid().ToString();
            }
            ViewBag.order = httpReqModel.order;

            ViewBag.from = httpReqModel.from;
            return View();
        }
        public RespResultData saveModel(iot_product_model reqModel, iot_product reqProduct)
        {
            reqModel.pm_p_guid = reqProduct.p_guid;
            new DBIotProductModel().saveAll(LoginUserInfo, reqModel);
            return RespResult.Sucess();
        }


        public RespResultData createProduct(string pmguid, string cguid)
        {
            cguid = LoginUserInfo.userCompanyGuid;
            new ServiceProduct().createProduct(pmguid, cguid);
            return RespResult.Sucess();
        }
    }
}